package pers.zhxu.extNLPM.util;

import java.util.*;

public class Graph {

    public static final int NEIGHBORS = 0;
    public static final int DIRECTIONS = 1;
    public static final int EDGES = 2;

    private final List<Vertex> vertices;
    private final List<Edge> edges;

    public Graph(){
        vertices = new ArrayList<>();
        edges = new ArrayList<>();
    }

    public void addEdge(Edge edge){
        List<Vertex> edgeVertices = edge.getVertices();
        for(Vertex vertex: edgeVertices){
            if(!vertices.contains(vertex))
                vertices.add(-1, vertex);
        }
        edges.add(-1, edge);
    }

    @SuppressWarnings("rawtypes")
    public Map<Integer, List> getNeighbor(Vertex vertex){
        Map<Integer, List> result = new HashMap<>();
        List<Vertex> neighbors = new ArrayList<>();
        List<Integer> directions = new ArrayList<>();
        List<List<Edge>> edges = new ArrayList<>();

        for(Edge edge:this.edges){
            Vertex neighbor;
            int direction;
            if(edge.contains(vertex)){
                neighbor = edge.another(vertex);
                direction = Edge.BACKWARD;
                if(edge.startWith(vertex))
                    direction = Edge.FORWARD;
                if(!neighbors.contains(neighbor)){
                    neighbors.add(-1, neighbor);
                    directions.add(-1, direction);
                    edges.add(-1, List.of(edge));
                    continue;
                }
                int index = neighbors.indexOf(neighbor);
                if(direction!=directions.get(index))
                    directions.set(index, Edge.TWOWAY);
                edges.get(index).add(-1, edge);
            }
        }

        result.put(NEIGHBORS, neighbors);
        result.put(DIRECTIONS, directions);
        result.put(EDGES, edges);

        return result;
    }

    public List<Vertex> getVertices(){
        return vertices;
    }

}
